package com.example.a001.part15;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.a001.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<View> views;
    private List<File> files;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3); // 修改为正确的布局文件

        ViewPager viewPager = findViewById(R.id.viewPager);
        views = new ArrayList<>();
        View view0 = getLayoutInflater().inflate(R.layout.listview, null);
        View view1 = getLayoutInflater().inflate(R.layout.listview, null);
        View view2 = getLayoutInflater().inflate(R.layout.listview, null);
        views.add(view0);
        views.add(view1);
        views.add(view2);

        // 设置适配器
        viewPager.setAdapter(new PageViewAdapter(views));

        files = new ArrayList<>();
        files.add(new File("第一个文件"));
        files.add(new File("第二个文件"));
        files.add(new File("第三个文件"));
        files.add(new File("第四个文件"));

        ListView listView = view0.findViewById(R.id.listview_phone);
        listView.setVisibility(ListView.VISIBLE);
        listView.setAdapter(new ListViewAdapter(files, getLayoutInflater()));
    }

    // 定义PageViewAdapter类
    private static class PageViewAdapter extends PagerAdapter {
        private final List<View> views;

        public PageViewAdapter(List<View> views) {
            this.views = views;
        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = views.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
